require("../less/my.less");

document.ready(function() {
    window.$utils.footerFn('my');
    //选择提示框
    window.$utils.selectTipFn('确认退出登录？');

    // axios设置服务器地址
    axios.defaults.baseURL = "http://139.9.177.51:8099";

    let petName = document.querySelector('#petName');
    let signText = document.querySelector('#signText');
    let profilePhoto = document.querySelector('#profilePhoto');
    let dynamicCount = document.querySelector('#dynamicCount');
    let times = document.querySelector('#times');
    let calorie = document.querySelector('#calorie');

    //初始化页面
    initializeMy();

    function initializeMy() {
        axios.get("/users/accountinfo?userId=" + localStorage.getItem('userId'))
            .then(function(res) {
                if (res.data.status == 0) {
                    // 里面数据不为null时再执行
                    //头像
                    let imgurl = res.data.data.imgurl;
                    if (imgurl) {
                        profilePhoto.src = imgurl;
                    }
                    //昵称
                    let nickname = res.data.data.nickname;
                    if (nickname) {
                        petName.textContent = nickname;
                    }
                    //个性签名
                    let sign = res.data.data.sign;
                    if (sign) {
                        signText.textContent = sign;
                    }
                }

            })
            .catch(function(err) {
                console.log(err);
            })

        axios.get("/users/mysportsBadge?userId=" + localStorage.getItem('userId'))
            .then(function(res) {
                dynamicCount.textContent = res.data.data.dynamicCount + '条';
                times.textContent = res.data.data.sports.times.toFixed();
                calorie.textContent = res.data.data.sports.calorie.toFixed();
            })
            .catch(function(err) {
                console.log(err);
            })
    }


    let consumer = document.querySelector('.consumer');
    //点击页面跳转
    consumer.addEventListener('click', function() {
        location.href = './infochange.html';
    })

    let quit = document.querySelector('#quit');
    let cancel = document.querySelector('#cancel');
    let confirm = document.querySelector('#confirm');
    let selectMask = document.querySelector('.selectMask');

    quit.addEventListener('click', function() {
        selectMask.style.display = 'block';
        cancel.addEventListener('click', function() {
            selectMask.style.display = 'none';
        })
        confirm.addEventListener('click', function() {
            location.href = './login.html';
            localStorage.removeItem('userId');
        })
    })
    document.querySelector('.data').addEventListener('click', function() {
        location.href = './sportsData.html';
    })
    document.querySelector('.badge').addEventListener('click', function() {
        location.href = './myBadge.html';
    })


})